// pages/pay/payMethod/payMethod.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      {value: 'weixin', img:"https://app-center.cdn.bcebos.com/appcenter/file/upload/a7c39683dd48fe24bc9df0c4ba12d260e5e9df79-72d0-4bed-9f72-fcfff23226d7.png",name: '微信支付', checked: 'true'},
      {value: 'zhifubao', img:"https://img.alicdn.com/tfs/TB1qEwuzrj1gK0jSZFOXXc7GpXa-32-32.ico", name: '支付宝支付'},
    ],
    //计时器
    minutes: 0,
    seconds: 0,
    isPaid: false,
    taskCode:"",
    task:{},
    task_img:[],
  },


  onLoad: function(options) {
    const taskVo = wx.getStorageSync('taskCode')
    console.log("123",taskVo)
    this.setData({
      "task": taskVo,
      minutes: 15,
      seconds: 0,
      'task_img':JSON.parse(taskVo.taskImageurl)
    });
    console.log("图片数据为：",this.data.task_img)
    this.startTimer();
  },

  //计时器
  startTimer: function() {
    this.timer = setInterval(() => {
      if (this.data.minutes === 0 && this.data.seconds === 0) {
        clearInterval(this.timer);
        if (!this.data.isPaid) {
          this.cancelOrder();
        }
      } else {
        let seconds = this.data.seconds - 1;
        let minutes = this.data.minutes;

        if (seconds < 0) {
          minutes--;
          seconds = 59;
        }

        this.setData({
          minutes: minutes,
          seconds: seconds
        });
      }
    }, 1000);
  },
  paymentSuccess: function() {//支付成功后，执行这个

    const task = wx.getStorageSync('taskCode')
    console.log("传递的任务编号为：",task);
    // 获取小程序实例
    const app = getApp();
    // 获取全局变量
    const globalUrl = app.globalData.globalUrl;
    const userInfo = wx.getStorageSync('UserData');
    console.log("获取任务编号："+this.data.taskCode);
    //发送请求到后端，用户下单此时的订单状态为未付款，通过id将用户状态设置为待接单
    wx.request({
      url: globalUrl+'/user/pay/taskPay',
      method: 'POST',
      header: {
        "token": userInfo.token,
        "taskCode":task.taskCode
      },
      success: (response) => {
        if(response.data.code === 200){ //成功添加地址列表到数据库
          //设置停止计时
          clearInterval(this.timer);
          this.setData({
            isPaid: true
          });
          console.log("获取到的数据为：",response.data);
          wx.showToast({
            title: '付款成功，返回首页',
            icon: 'loading',
            success: function() {
              setTimeout(function() {
                wx.switchTab({
                  url: '/pages/home/home' // 首页的路径
                });
              }, 500)
            }
          }); 
        }else if(response.data.code === 401){
          wx.showToast({
            title: '您还没有登录，请先登录',
            icon: 'none'
          })
        }else{
          wx.showToast({
            title: '您的网络有差异请稍后尝试！',
            icon: 'none'
          })
        }
      },
    });

  },
  cancelOrder: function() {//当计时器结束后执行此方法
    const task = wx.getStorageSync('taskCode')
    console.log("传递的任务编号为：",task);
    // 获取小程序实例
    const app = getApp();
    // 获取全局变量
    const globalUrl = app.globalData.globalUrl;
    const userInfo = wx.getStorageSync('UserData');
    //发送请求到后端，用户下单此时的订单状态为未付款，通过id将用户状态设置为待接单
    wx.request({
      url: globalUrl+'/user/pay/taskPayCancel',
      method: 'POST',
      header: {
        "token": userInfo.token,
        "taskCode":task.taskCode
      },
      success: (response) => {
        if(response.data.code === 200){ //成功添加地址列表到数据库
          wx.showToast({
            title: '订单已取消',
            icon: 'loading',
            success: function() {
              setTimeout(function() {
                wx.switchTab({
                  url: '/pages/home/home' // 首页的路径
                });
              }, 500)
            }
          }); 
        }else if(response.data.code === 401){
          wx.showToast({
            title: '您还没有登录，请先登录',
            icon: 'none'
          })
        }else{
          wx.showToast({
            title: '您的网络有差异请稍后尝试！',
            icon: 'none'
          })
        }
      },
    });

  },


  //支付方式选中
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    const items = this.data.items
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }
    this.setData({
      items
    })
  },




})